'''
Created on Mar 1, 2010

@author: Nick Heiner
'''

from google.appengine.ext import db

def range_validator(lower_bound, upper_bound):
    """ Make sure a value falls within the bounds. If a bound is None, the value will not be checked against it. """
    def validator(value):
        if (lower_bound is not None and value < lower_bound) or (upper_bound is not None and value >= upper_bound):
            raise ValueError("Value %s outside range (%s, %s]" % (value, lower_bound, upper_bound))
    return validator

class Item(db.Model):
    """ An item in the store """
    CSIN = db.IntegerProperty(required=True)
    name = db.StringProperty()
    price = db.IntegerProperty(validator=range_validator(0, None))
    quantity = db.IntegerProperty(validator=range_validator(0, None))
    image = db.LinkProperty(required=False)
    review = db.TextProperty(required=False) # only one review per item. sorry...
    
class InCart(db.Model):
    """ An entity relating a user to an item """ 
    owner = db.UserProperty(auto_current_user_add=True)
    item = db.ReferenceProperty(Item, required=True)
    quantity = db.IntegerProperty(required=True, validator=range_validator(0, None))